package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"hippo-fresh/hippofreshrpc/hippofreshrpc"
	"hippo-fresh/hippofreshrpc/internal/svc"
	"hippo-fresh/model/cache"
	"hippo-fresh/model/mysql"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopListLogic {
	return &ShopListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品列表
func (l *ShopListLogic) ShopList(in *hippofreshrpc.ShopListRequest) (*hippofreshrpc.ShopListResponse, error) {
	// todo: add your logic here and delete this line

	var list []*hippofreshrpc.ShopList

	key := fmt.Sprintf("shopList_%s", in)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if cacheData == "" {
		list = mysql.ShopList(mysql.DB, in)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Minute*30)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	return &hippofreshrpc.ShopListResponse{
		List: list,
	}, nil
}
